/**
 * 5-Stars Rating Component JS Stuff. 
 */

var CSS_POINTER = "pointer"; 
if(document.all) {
	CSS_POINTER = "hand";
} 

function saveRating(objId) {
	var obj = document.getElementById(objId);
	var hdnObj = document.getElementById(objId + "_hiddenVal");
	hdnObj.value = obj.src.substring(obj.src.lastIndexOf('/') + 1, obj.src.lastIndexOf('.'));
}

function paintRating(objId, contextPath, score) {
	var obj = document.getElementById(objId);
	switch(score) {
		case 1: obj.src = contextPath + "/faces/.componentsResourceLoader/rating/1.0.gif"; break;
		case 2: obj.src = contextPath + "/faces/.componentsResourceLoader/rating/2.0.gif"; break;
		case 3: obj.src = contextPath + "/faces/.componentsResourceLoader/rating/3.0.gif"; break;
		case 4: obj.src = contextPath + "/faces/.componentsResourceLoader/rating/4.0.gif"; break;
		case 5: obj.src = contextPath + "/faces/.componentsResourceLoader/rating/5.0.gif"; break;
		case 6: obj.src = contextPath + "/faces/.componentsResourceLoader/rating/0.5.gif"; break;
		case 7: obj.src = contextPath + "/faces/.componentsResourceLoader/rating/1.5.gif"; break;
		case 8: obj.src = contextPath + "/faces/.componentsResourceLoader/rating/2.5.gif"; break;
		case 9: obj.src = contextPath + "/faces/.componentsResourceLoader/rating/3.5.gif"; break;
		case 10: obj.src = contextPath + "/faces/.componentsResourceLoader/rating/4.5.gif"; break;
		default: obj.src = contextPath + "/faces/.componentsResourceLoader/rating/0.0.gif";
	}
}

function rateIt(objId, event) {
	var rating_mouseX = event.clientX ? event.clientX : event.pageX;
	var rating_elementX = findLeftOffset(document.getElementById(objId));
	var rating_offset = rating_mouseX - rating_elementX;

	if (rating_offset < 7) {
		return 6;
	} else if (rating_offset < 14) {
		return 1;
	} else if (rating_offset < 22) {
		return 7;
	} else if (rating_offset < 29) {
		return 2;
	} else if (rating_offset < 37) {
		return 8;
	} else if (rating_offset < 44) {
		return 3;
	} else if (rating_offset < 52) {
		return 9;
	} else if (rating_offset < 59) {
		return 4;
	} else if (rating_offset < 66) {
		return 10;
	} else if (rating_offset < 74) {
		return 5;
	} else {
		return -1;
	}
}

function finishRating(objId, contextPath) {
	var obj = document.getElementById(objId);
	var	hdnVal = document.getElementById(objId + "_hiddenVal");
	
	obj.src = contextPath + "/faces/.componentsResourceLoader/rating/" + hdnVal.value + ".gif";
	obj.style.cursor = 'default'; 
}

function findLeftOffset(obj) {
	var curleft = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft;
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft;
		}
	}
	return curleft;
}